package log.aop;

import java.lang.reflect.Method ;
import java.lang.reflect.Parameter ;

import org.aspectj.lang.JoinPoint ;
import org.aspectj.lang.annotation.After ;
import org.aspectj.lang.annotation.AfterThrowing ;
import org.aspectj.lang.annotation.Aspect ;
import org.aspectj.lang.annotation.Before ;
import org.aspectj.lang.annotation.Pointcut ;
import org.json.JSONObject ;
import org.springframework.cglib.core.MethodInfo ;
import org.springframework.stereotype.Component;;

/**
 * Description:日志切面类	
 * @author 80002165
 * @date 2016年12月16日 上午11:17:55
 */
@Aspect
@Component
public class LogAspect {
    /**
     * 测试抛异常加日志
     * @author 80002165
     * @date 2016年12月16日 上午11:29:59
     */
    @Pointcut("@annotation(log.aop.LogAnno)")
    public void throwPointCut(){}
    
    @Before("throwPointCut()")
    public void beforeException(JoinPoint point){
        System.out.println("异常之前") ;
    }
    
    @AfterThrowing("throwPointCut()")
    public void afterException(JoinPoint joinPoint){
        joinPoint.getArgs() ;
        
        joinPoint.getThis() ;
        joinPoint.getSignature() ;
        joinPoint.getStaticPart() ;//获得切点的切面
        System.out.println("异常之后") ;
        String methodName = joinPoint.getSignature().getName() ;//方法名
        Class targetClass = joinPoint.getTarget().getClass() ;
        
        Method method = null ;// targetClass.getMethod(methodName, parameterTypes) ;
        Method[] methods = targetClass.getMethods() ;
        for(Method m : methods){
            if(m.getName().equals(methodName)){
                method = m ;
                break;
            }
        }
        System.out.println(method.getParameterCount()) ;
        //方法的注解
        LogAnno logAnno = method.getAnnotation(LogAnno.class) ;
        String urlParamName = logAnno.paramName() ;
        Class<?>[] parameterClass = method.getParameterTypes() ;
        Object[] paramValues = joinPoint.getArgs() ;
        System.out.println(joinPoint.getStaticPart().toLongString()) ;
        System.out.println(joinPoint.getStaticPart().toShortString()) ;
        System.out.println(joinPoint.getSignature().getName()) ;//方法名 test
        System.out.println(joinPoint.getSignature().toLongString());//方法名全路径 public void log.aop.test.service.TestService.test(java.lang.String,java.lang.String,org.json.JSONObject)
        System.out.println(joinPoint.getSignature().toShortString());//方法名匹配名 TestService.test(..)
        int i=0 ;
        for(Class param : parameterClass){
            System.out.println(String.valueOf(param)) ;
//            System.out.println(param) ;
            if(param.getName().equals(urlParamName)){
                System.out.println("访问的url是："+paramValues[i].toString()) ;
            }
            i++ ;
        }
        
        System.out.println(method.getParameters()[0].getName()) ;
        JSONObject aram = (JSONObject) paramValues[2] ;
        System.out.println(aram.get("param")) ;
    }
}
